// Toast 工具函数，用于替代 antd 的 message
// 使用浏览器原生的通知或简单的 DOM 操作来显示消息

export interface ToastOptions {
  duration?: number;
  position?: 'top' | 'bottom' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
}

class Toast {
  private container: HTMLElement | null = null;

  constructor() {
    this.createContainer();
  }

  private createContainer() {
    if (typeof window === 'undefined') return;
    
    this.container = document.createElement('div');
    this.container.id = 'toast-container';
    this.container.style.cssText = `
      position: fixed;
      top: 20px;
      right: 20px;
      z-index: 9999;
      pointer-events: none;
    `;
    document.body.appendChild(this.container);
  }

  private createToast(message: string, type: 'success' | 'error' | 'warning' | 'info', options: ToastOptions = {}) {
    if (!this.container) return;

    const toast = document.createElement('div');
    const { duration = 3000 } = options;

    // 设置样式
    const bgColors = {
      success: '#10b981',
      error: '#ef4444',
      warning: '#f59e0b',
      info: '#3b82f6'
    };

    toast.style.cssText = `
      background: ${bgColors[type]};
      color: white;
      padding: 12px 16px;
      border-radius: 6px;
      margin-bottom: 8px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      font-size: 14px;
      font-weight: 500;
      max-width: 300px;
      word-wrap: break-word;
      pointer-events: auto;
      transform: translateX(100%);
      transition: transform 0.3s ease-in-out;
    `;

    toast.textContent = message;
    this.container.appendChild(toast);

    // 动画进入
    setTimeout(() => {
      toast.style.transform = 'translateX(0)';
    }, 10);

    // 自动移除
    setTimeout(() => {
      toast.style.transform = 'translateX(100%)';
      setTimeout(() => {
        if (this.container && this.container.contains(toast)) {
          this.container.removeChild(toast);
        }
      }, 300);
    }, duration);

    // 点击移除
    toast.addEventListener('click', () => {
      toast.style.transform = 'translateX(100%)';
      setTimeout(() => {
        if (this.container && this.container.contains(toast)) {
          this.container.removeChild(toast);
        }
      }, 300);
    });
  }

  success(message: string, options?: ToastOptions) {
    this.createToast(message, 'success', options);
  }

  error(message: string, options?: ToastOptions) {
    this.createToast(message, 'error', options);
  }

  warning(message: string, options?: ToastOptions) {
    this.createToast(message, 'warning', options);
  }

  info(message: string, options?: ToastOptions) {
    this.createToast(message, 'info', options);
  }
}

// 创建单例实例
const toast = new Toast();

// 导出方法
export const showSuccess = (message: string, options?: ToastOptions) => toast.success(message, options);
export const showError = (message: string, options?: ToastOptions) => toast.error(message, options);
export const showWarning = (message: string, options?: ToastOptions) => toast.warning(message, options);
export const showInfo = (message: string, options?: ToastOptions) => toast.info(message, options);

// 兼容 antd message 的接口
export const message = {
  success: showSuccess,
  error: showError,
  warning: showWarning,
  info: showInfo
};

export default toast;
